IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[User]'))
	BEGIN
		CREATE TABLE [RideEventDB].[dbo].[User](
			Id INT NOT NULL IDENTITY PRIMARY KEY,
			Code VARCHAR(255) NOT NULL,
			Name VARCHAR(50) NOT NULL,
			FirstName VARCHAR(50) NOT NULL,
			Birthday DATE NOT NULL,
			Address VARCHAR(120) NOT NULL,
			City VARCHAR(80) NOT NULL,
			Cp VARCHAR(5) NOT NULL,
			Email VARCHAR(80) NOT NULL,
			Pwd VARCHAR(MAX) NOT NULL,
			Img VARCHAR(100) NULL,
			Sexe CHAR(1) NOT NULL,
			DriveLicenceDate DATE NULL,
			TypeUserId INT NOT NULL,
			Activated BIT NOT NULL DEFAULT(0),
			DateCreation DATETIME NOT NULL,
			DateUpdate DATETIME NOT NULL
		);
		
		PRINT N' Table User was created.'
	END
ELSE
	BEGIN
		PRINT N' Table User exists yet.'
	END

	
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TypeUser]'))
	BEGIN
		CREATE TABLE [RideEventDB].[dbo].[TypeUser](
			Id INT NOT NULL IDENTITY PRIMARY KEY,
			Title VARCHAR(50) NOT NULL
		);
		
		PRINT N' Table TypeUser was created.'
	END
ELSE
	BEGIN
		PRINT N' Table TypeUser exists yet.'
	END

	
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Event]'))
	BEGIN
		CREATE TABLE [RideEventDB].[dbo].[Event](
			Id INT NOT NULL IDENTITY PRIMARY KEY,
			Title VARCHAR(50) NOT NULL,
			NamePlace VARCHAR(80) NOT NULL,
			Description VARCHAR(MAX) NULL,
			Img VARCHAR(100) NULL,
			Price INT NULL,
			Address VARCHAR(120) NOT NULL,
			City VARCHAR(80) NOT NULL,
			Cp VARCHAR(5) NOT NULL,
			DateBegin DATETIME NOT NULL,
			DateEnd DATETIME NOT NULL,
			SiteLink VARCHAR(100) NULL,
			Activated BIT NOT NULL DEFAULT(0),
			DateCreation DATETIME NOT NULL,
			DateUpdate DATETIME NOT NULL
		);
		
		PRINT N' Table Event was created.'
	END
ELSE
	BEGIN
		PRINT N' Table Event exists yet.'
	END
	
	
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Way]'))
	BEGIN
		CREATE TABLE [RideEventDB].[dbo].[Way](
			Id INT NOT NULL IDENTITY PRIMARY KEY,
			CodeWay VARCHAR(40) NOT NULL,
			MeetingHour TIME NOT NULL,
			MeetingPlace VARCHAR(250) NOT NULL,
			NumberPlaces INT NOT NULL,
			DriverId INT NOT NULL,
			EventId INT NOT NULL,
			VehiculeId INT NOT NULL,
			DateCreation DATETIME NOT NULL,
			DateUpdate DATETIME NOT NULL
		); 
		PRINT N' Table Way was created.'
	END
ELSE
	BEGIN
		PRINT N' Table Way Exists yet.'
	END

	
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[PassagerWay]'))
	BEGIN
		CREATE TABLE [RideEventDB].[dbo].[PassagerWay](
			Id INT NOT NULL IDENTITY PRIMARY KEY,
			PassagerId INT NOT NULL,
			WayId INT NOT NULL,
			DateCreation DATETIME NOT NULL,
			DateUpdate DATETIME NOT NULL
		);
		PRINT N' Table PassagerWay was created.'
	END
ELSE
	BEGIN
		PRINT N' Table PassagerWay Exists yet.'
	END

	
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Survey]'))
	BEGIN
		CREATE TABLE [RideEventDB].[dbo].[Survey](
			Id INT NOT NULL IDENTITY PRIMARY KEY,
			Note INT NOT NULL,
			Summery VARCHAR(MAX) NULL,
			PassagerId INT NOT NULL,
			WayId INT NOT NULL,
			DateCreation DATETIME NOT NULL,
			DateUpdate DATETIME NOT NULL
		);

		PRINT N' Table Survey was created.'
	END
ELSE
	BEGIN
		PRINT N' Table Survey Exists yet.'
	END


IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Vehicule]'))
	BEGIN
		CREATE TABLE [RideEventDB].[dbo].[Vehicule](
			Id INT NOT NULL IDENTITY PRIMARY KEY,
			Matricule VARCHAR(20) NOT NULL,
			NumberPlaces INT NOT NULL,
			DateCreation DATETIME NOT NULL,
			DateUpdate DATETIME NOT NULL
		);

		PRINT N' Table Vehicule was created.'
	END
ELSE
	BEGIN
		PRINT N' Table Vehicule Exists yet.'
	END

	
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Preference]'))
	BEGIN
		CREATE TABLE [RideEventDB].[dbo].[Preference](
			Id INT NOT NULL IDENTITY PRIMARY KEY,
			Age INT NULL,
			Smoker BIT DEFAULT(0),
			NumberPlaces INT NOT NULL,
			DriverStyleId INT NOT NULL,
			DateCreation DATETIME NOT NULL,
			DateUpdate DATETIME NOT NULL
		);

		PRINT N' Table Preference was created.'
	END
ELSE
	BEGIN
		PRINT N' Table Preference Exists yet.'
	END
	
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[PreferenceVehiculeWay]'))
	BEGIN
		CREATE TABLE [RideEventDB].[dbo].[PreferenceVehiculeWay](
			Id INT NOT NULL IDENTITY PRIMARY KEY,
			VehiculeId INT NOT NULL,
			PreferenceId INT NOT NULL,
			WayId INT NOT NULL,
			DateCreation DATETIME NOT NULL,
			DateUpdate DATETIME NOT NULL
		);

		PRINT N' Table PreferenceVehiculeWay was created.'
	END
ELSE
	BEGIN
		PRINT N' Table PreferenceVehiculeWay Exists yet.'
	END
	
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[DriverStyle]'))
	BEGIN
		CREATE TABLE [RideEventDB].[dbo].[DriverStyle](
			Id INT NOT NULL IDENTITY PRIMARY KEY,
			Title VARCHAR(50) NOT NULL,
			DateCreation DATETIME NOT NULL,
			DateUpdate DATETIME NOT NULL
		);

		PRINT N' Table DriverStyle was created.'
	END
ELSE
	BEGIN
		PRINT N' Table DriverStyle Exists yet.'
	END
/*FOREIGNKEY*/

IF (OBJECT_ID('FK_User_TypeUser', 'F') IS NULL)
	BEGIN
		ALTER TABLE [RideEventDB].[dbo].[User]
		ADD CONSTRAINT FK_User_TypeUser FOREIGN KEY (TypeUserId)
		REFERENCES [RideEventDB].[dbo].[TypeUser] (Id);
		
		PRINT N' Foreign key FK_User_TypeUser was created.'
	END
ELSE
	BEGIN
		PRINT N' Foreign key FK_User_TypeUser exists yet.'
	END
	
IF (OBJECT_ID('FK_Way_User', 'F') IS NULL)
	BEGIN	
		ALTER TABLE [RideEventDB].[dbo].[Way]
		ADD CONSTRAINT FK_Way_User FOREIGN KEY (DriverId)
		REFERENCES [RideEventDB].[dbo].[User] (Id);
		
		PRINT N' Foreign key FK_Way_User was created.'
	END
ELSE
	BEGIN
		PRINT N' Foreign key FK_Way_User exists yet.'
	END
	
IF (OBJECT_ID('FK_Way_Event', 'F') IS NULL)
	BEGIN		
		ALTER TABLE [RideEventDB].[dbo].[Way]
		ADD CONSTRAINT FK_Way_Event FOREIGN KEY (EventId)
		REFERENCES [RideEventDB].[dbo].[Event] (Id);
		
		PRINT N' Foreign key FK_Way_Event was created.'
	END
ELSE
	BEGIN
		PRINT N' Foreign key FK_Way_Event exists yet.'
	END
	
IF (OBJECT_ID('FK_Way_Vehicule', 'F') IS NULL)
	BEGIN
		ALTER TABLE [RideEventDB].[dbo].[Way]
		ADD CONSTRAINT FK_Way_Vehicule FOREIGN KEY (VehiculeId)
		REFERENCES [RideEventDB].[dbo].[Vehicule] (Id);
		
		PRINT N' Foreign key FK_Way_Vehicule was created.'
	END
ELSE
	BEGIN
		PRINT N' Foreign key FK_Way_Vehicule exists yet.'
	END
	
IF (OBJECT_ID('FK_PassagerWay_User', 'F') IS NULL)
	BEGIN
		ALTER TABLE [RideEventDB].[dbo].[PassagerWay]
		ADD CONSTRAINT FK_PassagerWay_User FOREIGN KEY (PassagerId)
		REFERENCES [RideEventDB].[dbo].[User] (Id);
		
		PRINT N' Foreign key FK_PassagerWay_User was created.'
	END
ELSE
	BEGIN
		PRINT N' Foreign key FK_PassagerWay_User exists yet.'
	END
	
IF (OBJECT_ID('FK_PassagerWay_Way', 'F') IS NULL)
	BEGIN
		ALTER TABLE [RideEventDB].[dbo].[PassagerWay]
		ADD CONSTRAINT FK_PassagerWay_Way FOREIGN KEY (WayId)
		REFERENCES [RideEventDB].[dbo].[Way] (Id);
		
		PRINT N' Foreign key FK_PassagerWay_Way was created.'
	END
ELSE
	BEGIN
		PRINT N' Foreign key FK_PassagerWay_Way exists yet.'
	END
	
IF (OBJECT_ID('FK_Survey_User', 'F') IS NULL)
	BEGIN
		ALTER TABLE [RideEventDB].[dbo].[Survey]
		ADD CONSTRAINT FK_Survey_User FOREIGN KEY (PassagerId)
		REFERENCES [RideEventDB].[dbo].[User] (Id);
		
		PRINT N' Foreign key FK_Survey_User was created.'
	END
ELSE
	BEGIN
		PRINT N' Foreign key FK_Survey_User exists yet.'
	END
	
IF (OBJECT_ID('FK_Survey_Way', 'F') IS NULL)
	BEGIN
		ALTER TABLE [RideEventDB].[dbo].[Survey]
		ADD CONSTRAINT FK_Survey_Way FOREIGN KEY (WayId)
		REFERENCES [RideEventDB].[dbo].[Way] (Id);
		
		PRINT N' Foreign key FK_Survey_Way was created.'
	END
ELSE
	BEGIN
		PRINT N' Foreign key FK_Survey_Way exists yet.'
	END
	
IF (OBJECT_ID('FK_Preference_DriverStyle', 'F') IS NULL)
	BEGIN
		ALTER TABLE [RideEventDB].[dbo].[Preference]
		ADD CONSTRAINT FK_Preference_DriverStyle FOREIGN KEY (DriverStyleId)
		REFERENCES [RideEventDB].[dbo].[DriverStyle] (Id);
		
		PRINT N' Foreign key FK_Preference_DriverStyle was created.'
	END
ELSE
	BEGIN
		PRINT N' Foreign key FK_Preference_DriverStyle exists yet.'
	END

IF (OBJECT_ID('FK_PreferenceVehiculeWay_Vehicule', 'F') IS NULL)
	BEGIN
		ALTER TABLE [RideEventDB].[dbo].[PreferenceVehiculeWay]
		ADD CONSTRAINT FK_PreferenceVehiculeWay_Vehicule FOREIGN KEY (VehiculeId)
		REFERENCES [RideEventDB].[dbo].[Vehicule] (Id);
		
		PRINT N' Foreign key FK_PreferenceVehiculeWay_Vehicule was created.'
	END
ELSE
	BEGIN
		PRINT N' Foreign key FK_PreferenceVehiculeWay_Vehicule exists yet.'
	END
	
IF (OBJECT_ID('FK_PreferenceVehiculeWay_Preference', 'F') IS NULL)
	BEGIN
		ALTER TABLE [RideEventDB].[dbo].[PreferenceVehiculeWay]
		ADD CONSTRAINT FK_PreferenceVehiculeWay_Preference FOREIGN KEY (PreferenceId)
		REFERENCES [RideEventDB].[dbo].[Preference] (Id);
		
		PRINT N' Foreign key FK_PreferenceVehiculeWay_Preference was created.'
	END
ELSE
	BEGIN
		PRINT N' Foreign key FK_PreferenceVehiculeWay_Preference exists yet.'
	END

IF (OBJECT_ID('FK_PreferenceVehiculeWay_Way', 'F') IS NULL)
	BEGIN
		ALTER TABLE [RideEventDB].[dbo].[PreferenceVehiculeWay]
		ADD CONSTRAINT FK_PreferenceVehiculeWay_Way FOREIGN KEY (WayId)
		REFERENCES [RideEventDB].[dbo].[Way] (Id);
		
		PRINT N' Foreign key FK_PreferenceVehiculeWay_Way was created.'
	END
ELSE
	BEGIN
		PRINT N' Foreign key FK_PreferenceVehiculeWay_Way exists yet.'
	END